package com.jdew.thkj.activity;

import android.app.Activity;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.jdew.thkj.R;
import com.jdew.thkj.adapter.BeautifulNumberAdapter;
import com.jdew.thkj.base.BaseActivity;
import com.jdew.thkj.bean.LiangHaoBean;
import com.jdew.thkj.manager.AppConfig;
import com.jdew.thkj.network.RetrofitManager;
import com.jdew.thkj.network.api.RequestApi;
import com.jdew.thkj.utils.MD5Utils;
import com.jdew.thkj.utils.RegexValidateUtils;
import com.scwang.smartrefresh.header.MaterialHeader;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.constant.SpinnerStyle;
import com.scwang.smartrefresh.layout.footer.BallPulseFooter;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

public class DetailsOfBeautifulNumberAwardsActivity extends BaseActivity {
    @Bind(R.id.back)
    ImageView back;
    @Bind(R.id.title)
    TextView title;
    @Bind(R.id.none)
    ImageView none;
    @Bind(R.id.message)
    TextView message;
    @Bind(R.id.none_view)
    RelativeLayout noneView;
    @Bind(R.id.recyclerview)
    RecyclerView recyclerview;
    @Bind(R.id.smartRefresh)
    SmartRefreshLayout smartRefresh;
    @Bind(R.id.footer)
    TextView footer;
    private BeautifulNumberAdapter adapter;
    private List<LiangHaoBean.DataBean> list = new ArrayList<>();
    private int page = 1;

    @Override
    public int bindLayout() {
        return R.layout.activity_beautify_number;
    }

    @Override
    public void initView(View view) {
        back.setVisibility(View.VISIBLE);
        title.setText("靓号奖励明细");
        sp = getSharedPreferences("user", Activity.MODE_PRIVATE);
        editor = sp.edit();
        adapter = new BeautifulNumberAdapter(this, list);
        setRcycleview(recyclerview, adapter);
        smartRefresh.setRefreshHeader(new MaterialHeader(this));
        smartRefresh.setRefreshFooter(new BallPulseFooter(this).setSpinnerStyle(SpinnerStyle.Scale));
        back.setOnClickListener(this);
        smartRefresh.setOnRefreshLoadMoreListener(new OnRefreshLoadMoreListener() {
            @Override
            public void onLoadMore(RefreshLayout refreshLayout) {
                page++;
                ToPromoteTheDetail();
                smartRefresh.finishLoadMore();
            }

            @Override
            public void onRefresh(RefreshLayout refreshLayout) {
                page = 1;
                ToPromoteTheDetail();
                smartRefresh.finishRefresh();
            }
        });
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.back: {
                finish();
            }
            break;

        }
    }

    public void ToPromoteTheDetail() {
        String timesamp = RegexValidateUtils.getTime13();
        String numsamp = RegexValidateUtils.getRandom10();
        HashMap<String, Object> map = new HashMap();
        map.put("loginId", sp.getString("userId", ""));
        map.put("pageIndex", page);
        RetrofitManager.getInstance(AppConfig.BASE_URL)
                .createReq(RequestApi.class).ToPromoteTheDetail(map, RegexValidateUtils.getUniquePsuedoID(), timesamp, numsamp, MD5Utils.getMD5(RegexValidateUtils.ASCIISort(timesamp + numsamp + RegexValidateUtils.getUniquePsuedoID() + sp.getString("SignToken", "") + "loginId" + sp.getString("userId", "") + "pageIndex" + page)).toUpperCase()).observeOn(AndroidSchedulers.mainThread()).subscribeOn(Schedulers.io()).subscribe(new Subscriber<LiangHaoBean>() {
            @Override
            public void onCompleted() {

            }

            @Override
            public void onError(Throwable e) {
            }

            @Override
            public void onNext(LiangHaoBean bean) {
                if (bean.getCode().equals("0")) {
                        if (null == bean.getData() && page == 1) {
                            noneView.setVisibility(View.GONE);
                            message.setText("您还没有靓号奖励记录哦");
                            return;
                        }
                        if (page == 1) {
                            list.clear();
                        }
                        list.addAll(bean.getData());
                        adapter.setDataList(list);
                        if (bean.getData().size() < 10 && bean.getData().size() > 0) {
                            smartRefresh.setEnableLoadMore(false);
                            footer.setText("已全部加载");
                        } else {
                            if (list.size() == 0 && page == 1) {
                                smartRefresh.setEnableLoadMore(false);
                                footer.setVisibility(View.GONE);
                                noneView.setVisibility(View.VISIBLE);
                                smartRefresh.setVisibility(View.GONE);
                                message.setText("您还没有靓号奖励记录哦");
                                return;
                            }
                        }
                        footer.setText("上拉加载更多");
                } else {
                    smartRefresh.setEnableLoadMore(false);
                    footer.setVisibility(View.GONE);
                    noneView.setVisibility(View.VISIBLE);
                    smartRefresh.setVisibility(View.GONE);
                    message.setText("您还没有靓号奖励记录哦");
                }
            }
        });

    }

    @Override
    protected void onResume() {
        super.onResume();
        ToPromoteTheDetail();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }
}
